#include <cjsonobject.h>
#include <cjsonerror.h>
#include <cjsonvalue_common.h>

CJsonObject::CJsonObject() :
  CJsonValue(JV_OBJECT)
{
}

CJsonObject::CJsonObject(const CJsonObject * value) :
  CJsonValue(JV_OBJECT)
{
  if (!value)
  {
    throw CJsonError(INVALID_PARAM);
  }
  value->GetValue((std::map<jstring, CJsonValue*>&)pares);
}

CJsonObject::~CJsonObject()
{
  Clear();
}

jstring CJsonObject::ToString() const
{
  jstring result = J("{");
  CPares::iterator it;
  bool start = true;
  for (it = pares.begin(); it != pares.end(); it++)
  {
    if (start)
    {
      start = false;
    }
    else
    {
      result += J(",");
    }
    result += J("\"") + (*it).first + J("\"") + J(":") + ((*it).second)->ToString();
  }
  result += J("}");
  return result;
}

void CJsonObject::AddPare(const jchar * name, const jchar * value)
{
  if (!name)
  {
    throw new CJsonError(INVALID_PARAM);
  }
  if (value)
  {
    pares[name] = new CJsonValueString(value);
  }
  else
  {
    pares[name] = new CJsonValueNull();
  }
}

void CJsonObject::AddPare(const jchar * name, jstring value)
{
  if (!name)
  {
    throw new CJsonError(INVALID_PARAM);
  }
  pares[name] = new CJsonValueString(value);
}

void CJsonObject::AddPare(const jchar * name, jint value)
{
  if (!name)
  {
    throw new CJsonError(INVALID_PARAM);
  }
  pares[name] = new CJsonValueNumber(value);
}

void CJsonObject::AddPare(const jchar * name, double value)
{
  if (!name)
  {
    throw new CJsonError(INVALID_PARAM);
  }
  pares[name] = new CJsonValueFloat(value);
}

void CJsonObject::AddPare(const jchar * name, bool value)
{
  if (!name)
  {
    throw new CJsonError(INVALID_PARAM);
  }
  pares[name] = new CJsonValueBool(value);
}

void CJsonObject::AddPare(const jchar * name, const CJsonValue * value)
{
  if (!name)
  {
    throw new CJsonError(INVALID_PARAM);
  }
  pares[name] = CopyValue(value);
}

void CJsonObject::AddPare(const jchar * name, const CJsonValueString * value)
{
  if (!name)
  {
    throw new CJsonError(INVALID_PARAM);
  }
  pares[name] = CopyValue((CJsonValue*)value);
}

void CJsonObject::AddPare(const jchar * name, const CJsonValueNumber * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(const jchar * name, const CJsonValueFloat * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(const jchar * name, const CJsonValueBool * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(const jchar * name, const CJsonValueNull * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(const jchar * name, const CJsonObject * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(const jchar * name, const CJsonArray * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(jstring name, const jchar * value)
{
  if (value)
  {
    pares[name] = new CJsonValueString(value);
  }
  else
  {
    pares[name] = new CJsonValueNull();
  }
}

void CJsonObject::AddPare(jstring name, jstring value)
{
  pares[name] = new CJsonValueString(value);
}

void CJsonObject::AddPare(jstring name, jint value)
{
  pares[name] = new CJsonValueNumber(value);
}

void CJsonObject::AddPare(jstring name, double value)
{
  pares[name] = new CJsonValueFloat(value);
}

void CJsonObject::AddPare(jstring name, bool value)
{
  pares[name] = new CJsonValueBool(value);
}

void CJsonObject::AddPare(jstring name, const CJsonValueNumber * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(jstring name, const CJsonValueFloat * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(jstring name, const CJsonValueBool * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(jstring name, const CJsonValueNull * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(jstring name, const CJsonObject * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(jstring name, const CJsonArray * value)
{
  AddPare(name, (CJsonValue*)value);
}

void CJsonObject::AddPare(jstring name, const CJsonValue * value)
{
  pares[name] = CopyValue(value);
}

const CJsonValue * CJsonObject::operator [] (const jchar * name)
{
  if (!name)
  {
    throw new CJsonError(INVALID_PARAM);
  }
  if (pares.find(name) == pares.end())
  {
    return NULL;
  }
  return pares[name];
}

const CJsonValue * CJsonObject::operator [] (const jstring name)
{
  if (pares.find(name) == pares.end())
  {
    return NULL;
  }
  return pares[name];
}

bool CJsonObject::Empty() const
{
  return pares.empty();
}

void CJsonObject::Clear()
{
  CPares::iterator it;
  CJsonValue *val;

  for (it = pares.begin(); it != pares.end(); it++)
  {
    val = (*it).second;
    if (val)
    {
      delete val;
    }
  }

  pares.clear();
}

void CJsonObject::Erase(const jchar * name)
{
  if (!name)
  {
    throw new CJsonError(INVALID_PARAM);
  }
  if (pares[name])
  {
    delete pares[name];
  }
  pares.erase(name);
}

void CJsonObject::Erase(const jstring name)
{
  if (pares[name])
  {
    delete pares[name];
  }
  pares.erase(name);
}

void CJsonObject::GetValue(std::map <jstring, CJsonValue*> & value) const 
{
  CPares::iterator it;
  for (it = pares.begin(); it != pares.end(); it++)
  {
    value[(*it).first] = CopyValue((*it).second);
  }
}

void CJsonObject::GetNames(std::vector <jstring> & names) const
{
  CPares::iterator it;
  for (it = pares.begin(); it != pares.end(); it++)
  {
    names.push_back((*it).first);
  }
}

